<?php
namespace app\index\domain;

class Search extends BaseList
{
    var $_needParam = ['limit', 'page'];
    var $_params    = [];
    var $_cache     = [];
	public function __construct($modelId)
	{
        $this->keyword = I('?get.keyword')? I('get.keyword'): '';
        $this->_initModelId = $modelId;
        $this->_needParam = array_flip($this->_needParam);
        $this->initParam();
    }

    /**
     * 获取列表中某个内容的数据对象
     */
    protected function createObj($objParam)
    {
        //return new Content($objParam['id']);
        return Classes::get('Content', $objParam['id']);
    }
    
    /**
     * 初始化参数组
     */
    public function initParam()
    {
        if(isset($this->_initParams)&& isset($this->_ininModelId)){
            $this->_params = $this->_initParams;
            $this->_modelId = $this->_initModelId;
            return;
        }
        $this->_initParams = [];
        if(I('get.page'))
            $this->_initParams['page'] = I('get.page');
        if(\think\Config::has('list_limit.search'))
            $limit = C('list_limit.search');
        else
            $limit = 15;
        $this->_initParams['limit'] = $limit;
        $this->_params = $this->_initParams;
        $this->_modelId = $this->_initModelId;
        return;
    }

    /**
     * 获取总页数
     */
    public function getPageCount()
    {
        $params = $this->_params;
        $cacheKey = md5('contentListCount'.serialize($params));
        $this->initParam();
        if(isset($this->_cache[$cacheKey]))
            return $this->_cache[$cacheKey];
        if($count = D('index/Content')->getSearchCount($this->_modelId, $this->keyword, $params)){
            return $this->_cache[$cacheKey] = ceil($count/$params['limit']);
        }
        return false;
    }

    /**
     * 自定义获取值
     */
    public function valueGet($key)
    {
        if(isset($this->getList()[$key])){
            $index = $this->getList()[$key];
            return $this->createObj($index);
        }
        if($result=$this->setParam($key))
            return $result;
        return null;
    }

    /**
     * 设置参数组
     */
    private function setParam($param)
    {
        if(!strpos($param, "_"))
            return false;
        list($paramKey, $paramValue) = explode('_', $param, 2);
        if(isset($this->_needParam[$paramKey])){
            $this->_params[$paramKey] = $paramValue;
            return $this;
        }
        if($paramKey == 'modelId')
            $this->_modelId = $paramValue;
        return false;
    }

    /**
     * 获取搜索页url
     */
    public function getUrl()
    {
        $param = [];
        if($this->_modelId)
            $param['mid'] = $this->_modelId;
        return U('index/search', $param);
    }

    /**
     * 设置只获取置顶
     * result object
     */
    public function getTop()
    {
        $this->_params['where']['top_ok']=1;
        return $this;
    }

    /**
     * 设置只获取推荐
     * result object
     */
    public function getRec()
    {
        $this->_params['where']['rec_ok']=1;
        return $this;
    }

    /**
     * 根据参数组获取列表
     */
    protected function getList()
    {
        $params = $this->_params;
        $cacheKey = md5('contentList'.serialize($params));
        if(\think\Config::get('auto_init_param')==1)
            $this->initParam();
        if(isset($this->_cache[$cacheKey]))
            return $this->_cache[$cacheKey];
        $contentList = D('index/Content')->getSearch($this->_modelId, $this->keyword, $params);
        if($contentList){
            return $this->_cache[$cacheKey] = $contentList;
        }
        return [];
    }
}

